//
//  TRPopAnimatedTransition.m
//  TRNavigationBar
//

#import "TRPopAnimatedTransition.h"

@implementation TRPopAnimatedTransition

- (void)animateTransition {
    [self.containerView insertSubview:self.toViewController.view belowSubview:self.fromViewController.view];
    
    // 是否隐藏tabBar
    BOOL isHideTabBar = self.toViewController.tabBarController && self.fromViewController.hidesBottomBarWhenPushed && self.toViewController.tr_captureImage;
    
    __block UIView *toView = nil;
    if (isHideTabBar) {
        UIImageView *captureView = [[UIImageView alloc] initWithImage:self.toViewController.tr_captureImage];
        captureView.frame = CGRectMake(0, 0, TR_SCREEN_WIDTH, TR_SCREEN_HEIGHT);
        [self.containerView insertSubview:captureView belowSubview:self.fromViewController.view];
        toView = captureView;
        self.toViewController.view.hidden = YES;
        self.toViewController.tabBarController.tabBar.hidden = YES;
    }else {
        toView = self.toViewController.view;
    }
    
    if (self.isScale) {
        self.shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TR_SCREEN_WIDTH, TR_SCREEN_HEIGHT)];
        self.shadowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6f];
        [toView addSubview:self.shadowView];
        
        if (@available(iOS 11.0, *)) {
            CGRect frame = toView.frame;
            frame.origin.x = TRConfigure.tr_translationX;
            frame.origin.y = TRConfigure.tr_translationY;
            frame.size.height -= 2 * TRConfigure.tr_translationY;
            toView.frame = frame;
        }else {
            toView.transform = CGAffineTransformMakeScale(TRConfigure.tr_scaleX, TRConfigure.tr_scaleY);
        }
    }else {
        self.fromViewController.view.frame = CGRectMake(- (0.3 * TR_SCREEN_WIDTH), 0, TR_SCREEN_WIDTH, TR_SCREEN_HEIGHT);
    }
    
    self.fromViewController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.fromViewController.view.layer.shadowOpacity = 0.2f;
    self.fromViewController.view.layer.shadowRadius = 4.0f;
    
    [UIView animateWithDuration:[self transitionDuration:self.transitionContext] animations:^{
        self.fromViewController.view.frame = CGRectMake(TR_SCREEN_WIDTH, 0, TR_SCREEN_WIDTH, TR_SCREEN_HEIGHT);
        if (self.isScale) {
            self.shadowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        }
        if (@available(iOS 11.0, *)) {
            toView.frame = CGRectMake(0, 0, TR_SCREEN_WIDTH, TR_SCREEN_HEIGHT);
        }else {
            toView.transform = CGAffineTransformIdentity;
        }
    } completion:^(BOOL finished) {
        [self completeTransition];
        if (isHideTabBar) {
            [toView removeFromSuperview];
            toView = nil;
            
            self.toViewController.view.hidden = NO;
            if (self.toViewController.navigationController.childViewControllers.count == 1) {
                self.toViewController.tabBarController.tabBar.hidden = NO;
            }
        }
        if (self.isScale) {
            [self.shadowView removeFromSuperview];
        }
    }];
}

@end
